package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.ClassReportReq;
import com.ruibang.glass.produce.domain.resp.MainIngredientInfo;
import com.ruibang.glass.produce.entity.ClassReport;
import com.ruibang.glass.produce.mapper.ClassReportMapper;
import com.ruibang.glass.produce.service.ClassReportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产-配料-班报填写 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-04-17
 */
@Service
public class ClassReportServiceImpl extends ServiceImpl<ClassReportMapper, ClassReport> implements ClassReportService {

    @Override
    public IPage<ClassReport> pageQuery(ClassReportReq classReportReq) {
        QueryWrapper<ClassReport> qw = new QueryWrapper<>();
        if (classReportReq.getStartDate() != null && classReportReq.getEndDate() != null) {
            qw.lambda().ge(ClassReport::getDate, classReportReq.getStartDate());
            qw.lambda().le(ClassReport::getDate, classReportReq.getEndDate());
        }
        qw.lambda().eq(ClassReport::getIsDelete, Constants.ZERO_STR);
        qw.lambda().orderByDesc(ClassReport::getDate);
        return this.page(new Page<>(classReportReq.getPageNo(), classReportReq.getPageSize()), qw);
    }

    @Override
    public void deleteById(String id) {
        lambdaUpdate().eq(ClassReport::getId, id).set(ClassReport::getIsDelete, Constants.ONE_STR).update();
    }

    /**
     * @Description: 班报每天任务
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-06 17:45
     */
    @Override
    public void executeClassReportTask(LocalDate now) {
        List<ClassReport> classReport = baseMapper.executeClassReportTask(now);
        if (CollectionUtils.isNotEmpty(classReport)) {
            //删除
            List<LocalDate> date = classReport.stream().map(ClassReport::getDate).collect(Collectors.toList());
            lambdaUpdate().in(ClassReport::getDate, date).remove();
            //新增
            this.saveBatch(classReport);
        }
    }

    /**
     * @Description: 获取数据最大日期
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-07 9:44
     */
    @Override
    public LocalDate getMaxDate() {
        return baseMapper.getMaxDate();
    }

    @Override
    public MainIngredientInfo getMainIngredientInfo(LocalDate date) {
        return baseMapper.getMainIngredientInfo(date);
    }
}
